package com.example.demo.services.system;


import cn.hutool.extra.spring.SpringUtil;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.demo.models.admin.Admin;
import com.example.demo.models.user.User;
import com.example.demo.models.base.Authenticatable;
import gaarason.database.contract.eloquent.Record;
import gaarason.database.exception.EntityNotFoundException;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Scope("prototype")
@Import(SpringUtil.class)
public class UserService {
    //线程变量，存放user实体类信息，即使是静态的与其他线程也是隔离的
    private static final ThreadLocal<UserService> userThreadLocal = new ThreadLocal<>();


    public static UserService getThreadInstance() {
        if (userThreadLocal.get() == null) {
            userThreadLocal.set(SpringUtil.getBean(UserService.class));
        }
        return userThreadLocal.get();
    }


    //用于存放用户的Map
    private Map<Class, Authenticatable> userMap = new HashMap<>();

    @Resource
    private Admin.Model adminModel;

    @Resource
    private User.Model userModel;

    //解析token
    public boolean parseToken(String token) {
        DecodedJWT jwt = JWTService.verify(token);
        //获得classId
        String classId = jwt.getClaim(JWTService.name2jwt("class")).asString();
//        System.out.println("classId:" + classId);
//        System.out.println("adminClassId:"+Admin.class.hashCode());
//        System.out.println("userClassId:"+User.class.hashCode());
        if (classId.equals(new Admin().getJWTClassId())) {
            //是管理员的class
            //获得用户id
            String id = jwt.getClaim(JWTService.name2jwt("id")).asString();
//            System.out.println("id:" + id);
            try {
                //查找到对应用户，并且解析登录
                Record<Admin, Long> user = adminModel.newQuery().findOrFail(id);
                System.out.println(user.getEntity().getNumber());
                setLoginUser(Admin.class, user.getEntity());
                return true;
            } catch (EntityNotFoundException e) {
                //System.out.println(e);
                return false;
            }
        } else if (classId.equals(new User().getJWTClassId())) {
            //是用户的class
            //获得用户id
            String id = jwt.getClaim(JWTService.name2jwt("id")).asString();
            try {
                //查找到对应用户，并且解析登录
                Record<User, Long> user = userModel.newQuery().findOrFail(id);
                setLoginUser(User.class, user.getEntity());
                return true;
            } catch (EntityNotFoundException e) {
                return false;
            }
        } else {

        }
        return false;
    }

    public Admin getAdmin() {
        return (Admin) getLoginUser(Admin.class);
    }

    public User getUser() {
        return (User) getLoginUser(User.class);
    }

    //从当前线程变量中获取用户信息
    public Authenticatable getLoginUser(Class clazz) {
        return userMap.get(clazz);
    }


    //为当前的线程变量赋值上用户信息
    public void setLoginUser(Class clazz, Authenticatable user) {
        userMap.put(clazz, user);
    }

    //清除线程变量
    public static void remove() {
        userThreadLocal.remove();
    }
}
